{extend name="public/base" /}

{block name="body"}
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body">

			<!--div class="layui-form layui-card-header layuiadmin-card-header-auto">
				<div class="layui-form-item">
				  <div class="layui-inline">
					<label class="layui-form-label">Tag关键词</label>
					<div class="layui-input-block">
					  <input type="text" name="tag" placeholder="请输入关键词" autocomplete="off" class="layui-input">
					</div>
				  </div>
				  <div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-forum-list" lay-submit lay-filter="Tag-link-search">
					  <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
					</button>
				  </div>
				</div>
			</div-->
            <div class="layui-form" wid100 lay-filter="">
				<div class="layui-upload">
					<button type="button" class="layui-btn layui-btn-normal" id="slid-add">添加</button>
					<table id="tag-link" lay-filter="tag-link"></table>
					<script type="text/html" id="barDemo">
						<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
						<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
					</script>
				</div> 
            </div>
            
          </div>
        </div>
      </div>
    </div>
  </div>
  
{/block}
{block name="js"}
  <script>
  layui.config({
    base: '/static/admin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','layer','table','form'], function(){
  var $ = layui.jquery
  ,layer = layui.layer
  ,table = layui.table
  ,form = layui.form;

  //第一个实例
  table.render({
    elem: '#tag-link'
    ,url: "{:url('TagLink/list')}" //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
      {type: 'numbers', fixed: 'left'}
	  ,{field: 'tag', title: 'Tag', width:200}
      ,{field: 'link', title: 'Link', minWidth:300}
	  ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
    ]]
	,limit: 15
	,text: '对不起，加载出现异常！'
  });

  form.on('submit(Tag-link-search)', function(data){
	  $.post("{:url('TagLink/list')}", {"tag":data.value}, function (data){
			if (data.code == -1){
				layer.open({content:data.msg,icon:5,anim:6});
			}
		}
	);
	//执行重载
	  table.reload('tag-link', {
		where: {tag: data.value}
		,page: {
                    curr: 1 //重新从第 1 页开始
                }
	  });
  });

  //监听行工具事件
  table.on('tool(tag-link)', function(obj){
    var data = obj.data;
	var tag = data.tag;
    if(obj.event === 'del'){
    	layer.confirm('真的删除行么', {icon: 3, title:'删除TagLink？'}, function(index){
			//提交 Ajax 成功后，静态更新表格中的数据
			$.post("{:url('TagLink/delete')}", {"tag":tag}, function(data){
				if (data.code == 0) {
					layer.msg(data.msg,{icon:6,time:2000}, function(){
						location.reload();
					});
				} else {
					layer.open({title:'删除失败',content:data.msg,icon:5,anim:6});
				}
			});
		});
	  layer.close(index);
    } else if(obj.event === 'edit'){
		layer.open({
		  type: 2,
		  title: '编辑TagLink',
		  content: 'edit.html?tag='+ tag,
		  maxmin: true,
		  area : ['400px' , '300px'],
		  btn: ['确定', '取消'],
          yes: function(index, layero){
		  var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'Tag-Link-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);
            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              $.ajax({
				type:"post",
				url:"{:url('TagLink/edit')}",
				data: field,
				daType:"json",
				success:function (data){
					if (data.code == 0) {
						layer.msg(data.msg,{icon:6,time:2000}, function(){
							location.reload();
						});
					} else {
						layer.open({title:'添加失败',content:data.msg,icon:5,anim:6});
					}
				}
			 });
		   layer.close(index);
		   });
		   submit.trigger('click');
		}
		});
    }
  });

  // 添加
  $('#slid-add').on('click', function(){
      layer.open({
		  type: 2,
		  title: '添加TagLink',
		  content: 'add.html',
		  maxmin: true,
		  area : ['400px' , '300px'],
		  btn: ['确定', '取消'],
          yes: function(index, layero){
		  var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'Tag-Link-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              
              //提交 Ajax 成功后，静态更新表格中的数据
              $.ajax({
				type:"post",
				url:"{:url('TagLink/add')}",
				data: field,
				daType:"json",
				success:function (data){
					if (data.code == 0) {
						layer.msg(data.msg,{icon:6,time:2000}, function(){
							location.reload();
						});
					} else {
						layer.open({title:'添加失败',content:data.msg,icon:5,anim:6});
					}
				}
			});
		   layer.close(index);
		   });
		   submit.trigger('click');
		  }
		});
    });
  
  });
</script>
{/block}